package com.zhongxi.guoyuan.view.personalCenter;

import android.app.Activity;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.application.MyApplication;
import com.zhongxi.guoyuan.base.BaseActivity;
import com.zhongxi.guoyuan.entity.BaseResponse;
import com.zhongxi.guoyuan.entity.user.center.UserCenterResult;
import com.zhongxi.guoyuan.fragment.RewardCoinOrder.RewardOrderHasNoApplyFragment;
import com.zhongxi.guoyuan.fragment.RewardCoinOrder.RewardOrderHasNoReceiverFragment;
import com.zhongxi.guoyuan.fragment.RewardCoinOrder.RewardOrderHasNoSendFragment;
import com.zhongxi.guoyuan.fragment.RewardCoinOrder.RewardOrderHasNoTokenFragment;
import com.zhongxi.guoyuan.url.Url;
import com.zhongxi.guoyuan.utils.GlideRoundTransform;
import com.zhongxi.guoyuan.view.update.UpdateActivity;

import java.util.LinkedList;
import java.util.List;

public class RewardCoinOrderActivity extends BaseActivity implements View.OnClickListener {

    private Button[] tabs;
    private int currentIndex, index;

    //地图导航
    private static final String APP_FOLDER_NAME = "Meirongyuan";
    private String mSDCardPath = null;
    public static List<Activity> activityList = new LinkedList<Activity>();

    /**
     * 内部TTS播报状态回传handler
     *//*
    private Handler ttsHandler = new Handler() {
        public void handleMessage(Message msg) {
            int type = msg.what;
            switch (type) {
                case BaiduNaviManager.TTSPlayMsgType.PLAY_START_MSG: {
                    break;
                }
                case BaiduNaviManager.TTSPlayMsgType.PLAY_END_MSG: {
                    break;
                }
                default:
                    break;
            }
        }
    };*/

    /**
     * 内部TTS播报状态回调接口
     *//*
    private BaiduNaviManager.TTSPlayStateListener ttsPlayStateListener = new BaiduNaviManager.TTSPlayStateListener() {

        @Override
        public void playEnd() {
        }

        @Override
        public void playStart() {
        }

    };*/
    private RelativeLayout relaContainer;
    private RewardOrderHasNoApplyFragment noPayFragment;
    private RewardOrderHasNoTokenFragment noTokenFragment;
    private RewardOrderHasNoSendFragment noSendFragment;
    private RewardOrderHasNoReceiverFragment noReceiverFragment;
    private Fragment[] fragments;
    private TextView tvUpdate;
    private TextView tvUserName;
    private TextView tvUserPhone;
    private ImageView ivUserHead;
    private TextView tvBonusPoint;
    private TextView tvIdentify;
    private ImageView ivBox;
    private String type;

    @Override
    protected void initView() {
        super.initView();
        setContentView(R.layout.activity_reward_coin_order);
        initTitle();
        tvTitle.setText("返银订单列表");
        /*initPageView();*/

        tvUpdate = (TextView) findViewById(R.id.tvUpdate);
        tvUserName = (TextView) findViewById(R.id.tvUserName);
        tvUserPhone = (TextView) findViewById(R.id.tvUserPhone);
        ivUserHead = (ImageView) findViewById(R.id.ivUserHead);
        tvBonusPoint = (TextView) findViewById(R.id.tvBonusPoint);
        tvIdentify = (TextView) findViewById(R.id.tvIdentify);

        ivBox = (ImageView) findViewById(R.id.ivBox);

    }

    private void initPageView() {
        relaContainer = (RelativeLayout)findViewById(R.id.relaContainer);
        tabs = new Button[4];
        tabs[0] = (Button) findViewById(R.id.bt0);
        tabs[1] = (Button) findViewById(R.id.bt1);
        tabs[2] = (Button) findViewById(R.id.bt2);
        tabs[3] = (Button) findViewById(R.id.bt3);
        tabs[0].setSelected(true);

        noPayFragment  = new RewardOrderHasNoApplyFragment();
        noSendFragment = new RewardOrderHasNoSendFragment();
        noTokenFragment = new RewardOrderHasNoTokenFragment();
        noReceiverFragment = new RewardOrderHasNoReceiverFragment();
        fragments = new Fragment[]{noPayFragment, noSendFragment, noReceiverFragment, noTokenFragment};

        getSupportFragmentManager().beginTransaction()
                .add(R.id.relaContainer, noPayFragment)
                .hide(noTokenFragment).commit();
    }

    @Override
    protected void initData() {
        super.initData();
        initPageView();
        getCenter();
    }


    /**
     * 获取用户信息
     */
    private void getCenter() {
        type = "center";
        getServer(Url.USER_CENTER, null, "", false, null, "get");
    }

    @Override
    protected void ProcessFailResult(String response) {
        super.ProcessFailResult(response);
        try {
            Gson gson = new Gson();
            BaseResponse result = gson.fromJson(response, BaseResponse.class);
            if (null != result.getSafeobj() && !"".equals(result.getSafeobj())) {
                MyApplication.getInstance().setSpValue(result.getSafeobj());
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    protected void processSuccessResult(String response) {
        super.processSuccessResult(response);
        Gson gson = new Gson();
        try{
            if("center".equals(type)) {
                UserCenterResult result = gson.fromJson(response, UserCenterResult.class);
                MyApplication.getInstance().setSpValue(result.getSafeobj());
                initViewData(result);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private void initViewData(UserCenterResult result) {
        try{
            if(null!=result.getUserInfo().getShopname()&&!"".equals(result.getUserInfo().getShopname())){
                tvUserName.setText(result.getUserInfo().getShopname());
            }else{
                tvUserName.setText(R.string.app_name);
            }
            if(null!=MyApplication.getInstance().getPhone()&&!"".equals(MyApplication.getInstance().getPhone())){
                tvUserPhone.setText(MyApplication.getInstance().parseSecutiryKey(result.getUserInfo().getMphone()));
            }else{
                tvUserPhone.setText("");
            }
            int point = 0;
            if (null != result.getRights() && result.getRights().size() > 0) {
                for (int i = 0; i < result.getRights().size(); i++) {
                    if (null != String.valueOf(result.getRights().get(i).getUsertype())) {
                        if (result.getRights().get(i).getUsertype() == 0) {  //股东
                            if (null != String.valueOf(result.getRights().get(i).getSharesnum()))
                                point += result.getRights().get(i).getSharesnum();
                        } else {                                                            //权益会员
                            if (null != String.valueOf(result.getRights().get(i).getSharesnum()))
                                point += result.getRights().get(i).getSharesnum();
                        }
                    }
                }
            }
            if (null != String.valueOf(point)) {
                tvBonusPoint.setText(String.valueOf(point));
            }else {
                tvBonusPoint.setText("0");
            }
            if(null!=result.getUserInfo().getAvatar()&&!"".equals(result.getUserInfo().getAvatar())){
                Glide.with(context).load(Url.BASE_URL+result.getUserInfo().getAvatar()).placeholder(R.mipmap.iv_eg).transform(new GlideRoundTransform(context, 20)).into(ivUserHead);
            }
            String sb = new String();
            if(null!=result.getUserInfo().getIsprivilege()){
                if("1".equals(result.getUserInfo().getIsprivilege())){
                    sb+="权益会员";
                    tvUpdate.setVisibility(View.GONE);
                }
            }
            if(null!=sb&&!"".equals(sb)){
                tvIdentify.setText(sb.toString());
            }else{
                tvIdentify.setText("普通会员");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        ivBack.setOnClickListener(this);
        tabs[0].setOnClickListener(this);
        tabs[1].setOnClickListener(this);
        tabs[2].setOnClickListener(this);
        tabs[3].setOnClickListener(this);
        ivBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, BoxActivity.class);
                intent.putExtra("index", 1);
                startActivity(intent);
            }
        });
        tvUpdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startAct(UpdateActivity.class);
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ivBack:
                finish();
                break;
            case R.id.bt0:
                index = 0;
                break;
            case R.id.bt1:
                index = 1;
                break;
            case R.id.bt2:
                index = 2;
                break;
            case R.id.bt3:
                index = 3;
                break;
        }
        if(currentIndex!=index){
            FragmentTransaction trx = getSupportFragmentManager().beginTransaction();
            trx.hide(fragments[currentIndex]);
            if(!fragments[index].isAdded()){
                trx.add(R.id.relaContainer, fragments[index]);
            }
            trx.show(fragments[index]).commit();
        }
        tabs[currentIndex].setSelected(false);
        tabs[index].setSelected(true);
        currentIndex = index;
    }
}
